package com.itheima.d4_create_thread;



import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * 目标：掌握线程的创建方式三：实现Callable接口。
 */
public class ThreadTest3 {
    public static void main(String[] args) throws Exception {
      /*  MyCallable myCallable = new MyCallable(100);
        FutureTask<Integer> ft = new FutureTask<>(myCallable);
        Thread th = new Thread(ft);
        th.start();
        Integer integer = ft.get();
        System.out.println(integer);
*/
        /*MyCallable mc = new MyCallable(100);
        FutureTask<Integer> integerFutureTask = new FutureTask<>(mc);
        Thread th = new Thread(integerFutureTask);
        th.start();
        System.out.println(integerFutureTask.get());*/

       /* MyCallable mc = new MyCallable(20);
        FutureTask<Integer> ft = new FutureTask<>(mc);
        Thread thread = new Thread(ft);
        thread.start();
        Integer integer = ft.get();
        System.out.println("integer = " + integer);*/
        MyCallable mc = new MyCallable(100);
        FutureTask<Integer> task = new FutureTask<>(mc);
        Thread thread = new Thread(task);
        thread.start();//启动该线程
        Integer integer = task.get();//获取现成的返回值
    }
}

class MyCallable implements Callable<Integer> {
    private int n;

    public MyCallable(int n) {
        this.n = n;
    }

    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += i;
        }
        return sum;
    }
}